#include <vector>

using namespace std;

class Solution {
public:

    // 1567. 乘积为正数的最长子数组长度
    // https://leetcode.cn/problems/maximum-length-of-subarray-with-positive-product/
    int getMaxLen(vector<int>& nums) {
        int n = nums.size();
        int ans = INT_MIN;
        vector<int> f(n+1), g(n+1);
        
        for (int i = 1; i<=n; ++i)
        {
            if (nums[i-1] > 0)
            {
                f[i] = f[i-1] + 1;
                g[i] = g[i-1] ? g[i-1]+1 : 0;
            }
            else if (nums[i-1] < 0)
            {
                f[i] = g[i-1] ? g[i-1]+1 : 0;
                g[i] = f[i-1] + 1;
            }
            ans = max(ans, f[i]);
        }
        return ans;
    }
};